; Whistle Windows 安装程序自定义脚本
; 这个文件被 package.json 中的 nsis.include 引用

; 引入必要的库
!include LogicLib.nsh

; 安装前检查和清理正在运行的进程
!macro customInit
  ; 使用系统命令检查 Whistle 进程
  ExpandEnvStrings $0 "%COMSPEC%"
  nsExec::ExecToStack '"$0" /c tasklist /fi "imagename eq Whistle.exe" | find /i "Whistle.exe"'
  Pop $1 ; 返回码
  Pop $2 ; 输出内容
  
  ${If} $1 == 0
    ; 找到了运行中的进程，询问用户
    MessageBox MB_YESNO|MB_ICONQUESTION "检测到 Whistle 正在运行。$\n$\n是否自动关闭并继续安装？$\n$\n点击'是'将自动关闭 Whistle，点击'否'将取消安装。" IDYES CloseProcess IDNO AbortInstall
    
    CloseProcess:
      DetailPrint "正在关闭 Whistle 进程..."
      
      ; 先尝试友好关闭 - 发送 WM_CLOSE 消息
      ExpandEnvStrings $0 "%COMSPEC%"
      nsExec::ExecToLog '"$0" /c taskkill /im "Whistle.exe" /t'
      
      ; 等待进程关闭
      Sleep 3000
      
      ; 检查进程是否已关闭
      nsExec::ExecToStack '"$0" /c tasklist /fi "imagename eq Whistle.exe" | find /i "Whistle.exe"'
      Pop $1
      Pop $2
      
      ${If} $1 == 0
        ; 进程仍在运行，强制终止
        DetailPrint "友好关闭失败，正在强制终止进程..."
        nsExec::ExecToLog '"$0" /c taskkill /f /im "Whistle.exe" /t'
        Sleep 2000
      ${Else}
        DetailPrint "Whistle 进程已成功关闭"
      ${EndIf}
      
      ; 额外检查：关闭可能的相关 Node.js 进程（可选，谨慎使用）
      ; nsExec::ExecToLog '"$0" /c taskkill /f /fi "windowtitle eq *whistle*" /t'
      
      Goto ContinueInstall
      
    AbortInstall:
      MessageBox MB_OK|MB_ICONSTOP "安装已取消。$\n$\n请手动关闭 Whistle 后重新运行安装程序。"
      Abort
      
    ContinueInstall:
      DetailPrint "准备继续安装..."
  ${Else}
    DetailPrint "未检测到运行中的 Whistle 进程，继续安装"
  ${EndIf}
!macroend

; 自定义安装宏
!macro customInstall
  ; 创建开始菜单快捷方式
  CreateDirectory "$SMPROGRAMS\Whistle"
  CreateShortCut "$SMPROGRAMS\Whistle\Whistle.lnk" "$INSTDIR\Whistle.exe"
  CreateShortCut "$SMPROGRAMS\Whistle\Uninstall.lnk" "$INSTDIR\Uninstall.exe"
  
  ; 创建桌面快捷方式
  CreateShortCut "$DESKTOP\Whistle.lnk" "$INSTDIR\Whistle.exe"
  
  ; 写入卸载信息到注册表
  WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Whistle" \
                   "DisplayName" "Whistle"
  WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Whistle" \
                   "UninstallString" "$INSTDIR\Uninstall.exe"
  WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Whistle" \
                   "DisplayIcon" "$INSTDIR\Whistle.exe"
  WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Whistle" \
                   "Publisher" "Whistle Team"
  WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Whistle" \
                   "DisplayVersion" "${VERSION}"
  WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Whistle" \
                     "NoModify" 1
  WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Whistle" \
                     "NoRepair" 1
!macroend

; 自定义卸载宏
!macro customUnInstall
  ; 删除开始菜单快捷方式
  Delete "$SMPROGRAMS\Whistle\Whistle.lnk"
  Delete "$SMPROGRAMS\Whistle\Uninstall.lnk"
  RMDir "$SMPROGRAMS\Whistle"
  
  ; 删除桌面快捷方式
  Delete "$DESKTOP\Whistle.lnk"
  
  ; 删除注册表项
  DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Whistle"
!macroend

; 安装完成后的操作
!macro customInstallMode
  ; 设置安装模式选择
  SetOutPath "$INSTDIR"
!macroend

; 自定义安装选项
!macro customInstallOptions
  ; 允许用户选择安装目录
  !define MUI_PAGE_DIRECTORY
  !define MUI_PAGE_HEADER_TEXT "选择安装位置"
  !define MUI_PAGE_HEADER_SUBTEXT "选择 Whistle 的安装目录"
!macroend
